home *** CD-ROM | disk | FTP | other *** search
Wrap
Public Class ClipboardForm Inherits System.Windows.Forms.Form #Region " Windows Form Designer generated code " Public Sub New() MyBase.New() 'This call is required by the Windows Form Designer. InitializeComponent() 'Add any initialization after the InitializeComponent() call End Sub 'Form overrides dispose to clean up the component list. Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean) If disposing Then If Not (components Is Nothing) Then components.Dispose() End If End If MyBase.Dispose(disposing) End Sub Friend WithEvents TextBox1 As System.Windows.Forms.TextBox Friend WithEvents Label1 As System.Windows.Forms.Label Friend WithEvents Label2 As System.Windows.Forms.Label Friend WithEvents RichTextBox1 As System.Windows.Forms.RichTextBox Friend WithEvents btnCopyTB As System.Windows.Forms.Button Friend WithEvents btnPasteTB As System.Windows.Forms.Button Friend WithEvents btnPasteRTB As System.Windows.Forms.Button Friend WithEvents btnCopyRTB As System.Windows.Forms.Button Friend WithEvents PictureBox1 As System.Windows.Forms.PictureBox Friend WithEvents btnCopyPB As System.Windows.Forms.Button Friend WithEvents btnPastePB As System.Windows.Forms.Button Friend WithEvents btnShowForm As System.Windows.Forms.Button Friend WithEvents btnEnumFormats As System.Windows.Forms.Button Friend WithEvents btnClearPB As System.Windows.Forms.Button 'Required by the Windows Form Designer Private components As System.ComponentModel.Container 'NOTE: The following procedure is required by the Windows Form Designer 'It can be modified using the Windows Form Designer. 'Do not modify it using the code editor. Friend WithEvents btnPasteCsv As System.Windows.Forms.Button <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent() Me.btnShowForm = New System.Windows.Forms.Button() Me.btnPasteRTB = New System.Windows.Forms.Button() Me.TextBox1 = New System.Windows.Forms.TextBox() Me.btnCopyPB = New System.Windows.Forms.Button() Me.btnEnumFormats = New System.Windows.Forms.Button() Me.btnCopyTB = New System.Windows.Forms.Button() Me.PictureBox1 = New System.Windows.Forms.PictureBox() Me.btnCopyRTB = New System.Windows.Forms.Button() Me.RichTextBox1 = New System.Windows.Forms.RichTextBox() Me.btnPasteTB = New System.Windows.Forms.Button() Me.btnPastePB = New System.Windows.Forms.Button() Me.Label1 = New System.Windows.Forms.Label() Me.Label2 = New System.Windows.Forms.Label() Me.btnClearPB = New System.Windows.Forms.Button() Me.btnPasteCsv = New System.Windows.Forms.Button() Me.SuspendLayout() ' 'btnShowForm ' Me.btnShowForm.Location = New System.Drawing.Point(24, 336) Me.btnShowForm.Name = "btnShowForm" Me.btnShowForm.Size = New System.Drawing.Size(312, 32) Me.btnShowForm.TabIndex = 5 Me.btnShowForm.Text = "Show another instance of this form" ' 'btnPasteRTB ' Me.btnPasteRTB.Location = New System.Drawing.Point(344, 232) Me.btnPasteRTB.Name = "btnPasteRTB" Me.btnPasteRTB.Size = New System.Drawing.Size(64, 32) Me.btnPasteRTB.TabIndex = 3 Me.btnPasteRTB.Text = "Paste" ' 'TextBox1 ' Me.TextBox1.AllowDrop = True Me.TextBox1.Location = New System.Drawing.Point(24, 32) Me.TextBox1.Multiline = True Me.TextBox1.Name = "TextBox1" Me.TextBox1.ScrollBars = System.Windows.Forms.ScrollBars.Vertical Me.TextBox1.Size = New System.Drawing.Size(312, 120) Me.TextBox1.TabIndex = 0 Me.TextBox1.Text = "" ' 'btnCopyPB ' Me.btnCopyPB.Location = New System.Drawing.Point(624, 32) Me.btnCopyPB.Name = "btnCopyPB" Me.btnCopyPB.Size = New System.Drawing.Size(64, 32) Me.btnCopyPB.TabIndex = 3 Me.btnCopyPB.Text = "Copy" ' 'btnEnumFormats ' Me.btnEnumFormats.Location = New System.Drawing.Point(432, 336) Me.btnEnumFormats.Name = "btnEnumFormats" Me.btnEnumFormats.Size = New System.Drawing.Size(184, 32) Me.btnEnumFormats.TabIndex = 5 Me.btnEnumFormats.Text = "Display clipboard formats" ' 'btnCopyTB ' Me.btnCopyTB.Location = New System.Drawing.Point(344, 32) Me.btnCopyTB.Name = "btnCopyTB" Me.btnCopyTB.Size = New System.Drawing.Size(64, 32) Me.btnCopyTB.TabIndex = 3 Me.btnCopyTB.Text = "Copy" ' 'PictureBox1 ' Me.PictureBox1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D Me.PictureBox1.Location = New System.Drawing.Point(432, 32) Me.PictureBox1.Name = "PictureBox1" Me.PictureBox1.Size = New System.Drawing.Size(180, 292) Me.PictureBox1.TabIndex = 4 Me.PictureBox1.TabStop = False ' 'btnCopyRTB ' Me.btnCopyRTB.Location = New System.Drawing.Point(344, 192) Me.btnCopyRTB.Name = "btnCopyRTB" Me.btnCopyRTB.Size = New System.Drawing.Size(64, 32) Me.btnCopyRTB.TabIndex = 3 Me.btnCopyRTB.Text = "Copy" ' 'RichTextBox1 ' Me.RichTextBox1.AllowDrop = True Me.RichTextBox1.Location = New System.Drawing.Point(24, 192) Me.RichTextBox1.Name = "RichTextBox1" Me.RichTextBox1.Size = New System.Drawing.Size(312, 128) Me.RichTextBox1.TabIndex = 2 Me.RichTextBox1.Text = "" ' 'btnPasteTB ' Me.btnPasteTB.Location = New System.Drawing.Point(344, 72) Me.btnPasteTB.Name = "btnPasteTB" Me.btnPasteTB.Size = New System.Drawing.Size(64, 32) Me.btnPasteTB.TabIndex = 3 Me.btnPasteTB.Text = "Paste" ' 'btnPastePB ' Me.btnPastePB.Location = New System.Drawing.Point(624, 72) Me.btnPastePB.Name = "btnPastePB" Me.btnPastePB.Size = New System.Drawing.Size(64, 32) Me.btnPastePB.TabIndex = 3 Me.btnPastePB.Text = "Paste" ' 'Label1 ' Me.Label1.Location = New System.Drawing.Point(24, 8) Me.Label1.Name = "Label1" Me.Label1.Size = New System.Drawing.Size(160, 16) Me.Label1.TabIndex = 1 Me.Label1.Text = "A TextBox control" ' 'Label2 ' Me.Label2.Location = New System.Drawing.Point(24, 168) Me.Label2.Name = "Label2" Me.Label2.Size = New System.Drawing.Size(160, 16) Me.Label2.TabIndex = 1 Me.Label2.Text = "A RichTextBox control" ' 'btnClearPB ' Me.btnClearPB.Location = New System.Drawing.Point(624, 120) Me.btnClearPB.Name = "btnClearPB" Me.btnClearPB.Size = New System.Drawing.Size(64, 32) Me.btnClearPB.TabIndex = 3 Me.btnClearPB.Text = "Clear" ' 'btnPasteCsv ' Me.btnPasteCsv.Location = New System.Drawing.Point(344, 112) Me.btnPasteCsv.Name = "btnPasteCsv" Me.btnPasteCsv.Size = New System.Drawing.Size(64, 40) Me.btnPasteCsv.TabIndex = 7 Me.btnPasteCsv.Text = "Paste CSV" ' 'ClipboardForm ' Me.AutoScaleBaseSize = New System.Drawing.Size(7, 17) Me.ClientSize = New System.Drawing.Size(696, 381) Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.btnPasteCsv, Me.btnClearPB, Me.btnEnumFormats, Me.btnShowForm, Me.btnCopyPB, Me.btnPastePB, Me.PictureBox1, Me.btnPasteRTB, Me.btnCopyRTB, Me.btnPasteTB, Me.btnCopyTB, Me.RichTextBox1, Me.Label2, Me.Label1, Me.TextBox1}) Me.Font = New System.Drawing.Font("Microsoft Sans Serif", 11.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.Name = "ClipboardForm" Me.Text = "Clipboard demo" Me.ResumeLayout(False) End Sub #End Region ' show another instance of this form Private Sub btnShowForm_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnShowForm.Click Dim frm As New ClipboardForm() frm.Show() End Sub ' copy the contents of the TextBox into the Clipboard Private Sub btnCopyTB_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCopyTB.Click CopyFromTextBox(TextBox1) End Sub ' paste the contents of the Clipboard into the TextBox Private Sub btnPasteTB_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPasteTB.Click PasteIntoTextBox(TextBox1) End Sub ' copy the contents of the RTB control into the Clipboard Private Sub btnCopyRTB_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCopyRTB.Click CopyFromRichTextBox(RichTextBox1) End Sub ' paste the contents of the Clipboard into the RTB control Private Sub btnPasteRTB_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPasteRTB.Click PasteIntoRichTextBox(RichTextBox1) End Sub ' show all clipboard formats Private Sub btnEnumFormats_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEnumFormats.Click EnumClipboardFormats() End Sub ' copy the contents of the PictureBox into the clipboard Private Sub btnCopyPB_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCopyPB.Click If PictureBox1.Image Is Nothing Then MessageBox.Show("First paste an image into the PictureBox", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation) Exit Sub End If CopyFromPictureBox(PictureBox1) End Sub ' paste the contents of the clipboard into the PictureBox Private Sub btnPastePB_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPastePB.Click PasteIntoPictureBox(PictureBox1) End Sub ' clear the contents of the PictureBox Private Sub btnClearPB_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClearPB.Click PictureBox1.Image = Nothing End Sub ' a reusable routine the copies the contents of a TextBox ' into the Clipboard Sub CopyFromTextBox(ByVal tb As TextBox) ' Copy the TextBox's selected text to the clipboard. Dim t As String = tb.SelectedText ' Copy the entire Text, if no text is selected. If t = "" Then t = tb.Text ' Proceed only if there is something to be copied. If t <> "" Then Clipboard.SetDataObject(t) End If End Sub ' a reusable routine the pastes the contents of the Clipboard ' into a TextBox control Sub PasteIntoTextBox(ByVal tb As TextBox) ' Get the data in the clipboard. Dim data As IDataObject = Clipboard.GetDataObject ' Check if there is any data in text format, converting it if necessary. If data.GetDataPresent(DataFormats.Text, True) Then ' If yes, paste into the selection. tb.SelectedText = data.GetData(DataFormats.Text, True).ToString End If End Sub ' a reusable routine the copies the contents of a RichTextBox ' into the Clipboard Sub CopyFromRichTextBox(ByVal rtf As RichTextBox) ' copy the RichTextBox's selected text to the clipboard Dim data As New DataObject() Dim t As String = rtf.SelectedRtf ' or the entire Text, if no text is selected If t = "" Then t = rtf.Rtf ' do the copy only if there is something to be copied If t <> "" Then data.SetData(DataFormats.Rtf, t) ' do it again with the plain text t = rtf.SelectedText ' Copy the entire Text, if no text is selected. If t = "" Then t = rtf.Text ' Proceed only if there is something to be copied. If t <> "" Then data.SetData(DataFormats.Text, t) End If ' Pass True to make the data available to other applications. Clipboard.SetDataObject(data, True) End Sub ' a reusable routine the pastes the contents of the Clipboard ' into a RichTextBox control Sub PasteIntoRichTextBox(ByVal rtf As RichTextBox) ' Get the data in the clipboard. Dim data As IDataObject = Clipboard.GetDataObject ' Check if there is any data in RTF format, ' WITHOUT attempting a conversion If data.GetDataPresent(DataFormats.Rtf, False) Then ' If available, paste into the RTF selection. rtf.SelectedRtf = data.GetData(DataFormats.Rtf).ToString ElseIf data.GetDataPresent(DataFormats.Text, True) Then ' Else, attempt to get data in plain text format. rtf.SelectedText = data.GetData(DataFormats.Text, True).ToString End If End Sub ' a routine the copies the contents of a PictureBox ' into the Clipboard Sub CopyFromPictureBox(ByVal pb As PictureBox) Clipboard.SetDataObject(PictureBox1.Image) End Sub ' a reusable routine the pastes the contents of the Clipboard ' into a PictureBox control Sub PasteIntoPictureBox(ByVal pb As PictureBox) ' Get the data in the clipboard. Dim data As IDataObject = Clipboard.GetDataObject If data.GetDataPresent(DataFormats.Bitmap) Then pb.Image = CType(data.GetData(DataFormats.Bitmap), Image) ElseIf data.GetDataPresent(DataFormats.Tiff) Then pb.Image = CType(data.GetData(DataFormats.Tiff), Image) End If End Sub ' enumerate all Clipboard formats Sub EnumClipboardFormats() Dim t As String Dim msg As String ' Prepare the list of formats currently in the clipboard. For Each t In Clipboard.GetDataObject.GetFormats msg &= t & ControlChars.CrLf Next ' Show the list. MessageBox.Show(msg, "Current Clipboard formats") End Sub ' this event fires during a D&D operation Private Sub TextBox1_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles TextBox1.DragEnter, RichTextBox1.DragEnter ' Check that the user is dragging a text If e.Data.GetDataPresent(DataFormats.Text, True) Then If CBool(e.KeyState And 8) Then ' If Ctrl key is pressed, this is a copy operation. e.Effect = e.AllowedEffect And DragDropEffects.Copy Else ' Otherwise it is a move operation. e.Effect = e.AllowedEffect And DragDropEffects.Move End If Else ' reject any other type e.Effect = DragDropEffects.None End If End Sub ' this event fires during a D&D operation Private Sub TextBox1_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles TextBox1.DragDrop, RichTextBox1.DragDrop ' Exit if data isn't in text format If Not e.Data.GetDataPresent(DataFormats.Text, True) Then Exit Sub ' Decide whether is a copy or move operation. If CBool(e.KeyState And 8) Then e.Effect = DragDropEffects.Copy Else e.Effect = DragDropEffects.Move End If If (TypeOf sender Is RichTextBox) AndAlso e.Data.GetDataPresent(DataFormats.Rtf) Then ' If control is a RichTextBox and data is in RTF format, get it. Dim rtf As RichTextBox = DirectCast(sender, RichTextBox) rtf.SelectedRtf = e.Data.GetData(DataFormats.Rtf).ToString Else ' Otherwise paste plain text. Dim tbase As TextBoxBase = DirectCast(sender, TextBoxBase) tbase.SelectedText = e.Data.GetData(DataFormats.Text).ToString End If End Sub ' this event can initiate a D&D operation Private Sub TextBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles TextBox1.MouseMove, RichTextBox1.MouseMove ' Exit if no button is pressed. If e.Button = 0 Then Exit Sub ' Get a reference to the control, exit if it contains no text. Dim tbase As TextBoxBase = DirectCast(sender, TextBoxBase) If tbase.TextLength = 0 Then Exit Sub ' Exit if the cursor is inside the control's borders. If e.X >= 0 And e.X < tbase.Width And e.Y >= 0 And e.Y < tbase.Height Then Exit Sub ' the mouse is being dragged outside the control's client area, ' so we can start a drag-and-drop operation. ' Create a standalone DataObject. Dim data As New DataObject() ' Store the selected text, or all the text if no selection. If tbase.SelectionLength > 0 Then data.SetData(DataFormats.Text, tbase.SelectedText) Else data.SetData(DataFormats.Text, tbase.Text) End If ' If the control is a RichTextBox, store also the selected Rtftext ' or its entire contents if no selection. If TypeOf sender Is RichTextBox Then Dim rtfbox As RichTextBox = DirectCast(sender, RichTextBox) If rtfbox.SelectionLength > 0 Then data.SetData(DataFormats.Rtf, rtfbox.SelectedRtf) Else data.SetData(DataFormats.Rtf, rtfbox.Rtf) End If End If ' Don't accept dropped data while the operation is running. Dim saveAllowDrop As Boolean = tbase.AllowDrop tbase.AllowDrop = False ' Start the drag operation - wait until it's completed. Dim effect As DragDropEffects = DragDropEffects.Copy Or DragDropEffects.Move effect = tbase.DoDragDrop(data, effect) ' Delete the text if it was a move operation. If effect = DragDropEffects.Move Then If tbase.SelectionLength > 0 Then tbase.SelectedText = "" Else tbase.Text = "" End If End If ' Re-enable this control as a D&D target. tbase.AllowDrop = saveAllowDrop End Sub ' this event serves to cancel a D&D if the user presses the Esc key Private Sub TextBox1_QueryContinueDrag(ByVal sender As Object, ByVal e As System.Windows.Forms.QueryContinueDragEventArgs) Handles TextBox1.QueryContinueDrag, RichTextBox1.QueryContinueDrag If e.EscapePressed Then e.Action = DragAction.Cancel End Sub End Class